package de.urwalking.legobaukasten;

import java.util.ArrayList;
import java.util.Iterator;

import android.util.Log;

import com.threed.jpct.Polyline;
import com.threed.jpct.RGBColor;
import com.threed.jpct.SimpleVector;

public class URWwall {

	public URWwall()
	{
		//TODO
	}
	
	
	public ArrayList<Polyline> createWalls(ArrayList<URWnode> shopsold, ArrayList<URWedge> edges){
		
		ArrayList<URWnode> shops = shopsold;
		
		ArrayList<Polyline> walls = new ArrayList<Polyline>();
		
		float length1 = 10000;
		float actualLength = 0;
		int index;
		
		
		Iterator<URWnode> sIter = shops.iterator();
		while(sIter.hasNext()) {
			URWnode e = sIter.next();
			
			int[] wall = new int[]
			{
				e.x, e.y, 0, 0
			};
			
			sIter.remove();
			
				for(URWnode f : shops) {
					actualLength = linelength(wall[0], wall[1], f.x, f.y);
					
					
					if ((wall[0]!=f.x || wall[1]!=f.x) && actualLength < length1 && !intersection(wall[0], wall[1], f.x, f.y, edges)) {

						length1 = actualLength;
						wall[2]= f.x;
						wall[3]= f.y;
					}
					
				}
			if (wall[3] != 0 && wall[2] != 0){
				walls.add(createWall(wall[0], wall[1], wall[2], wall[3]));
			}
			length1 = 10000;
		}
		
		return walls;
	}
	
	
	public boolean intersection(int x1, int y1, int x2, int y2, ArrayList<URWedge> edges) {
			for (URWedge k : edges) {
				if (k.initialized) {
					if (linesIntersect(x1, y1, x2, y2, k.source.x, k.source.y, k.sink.x, k.sink.y)) {
						return true;
					}
				}
			}
		return false;
	}
	
	
	
	//From Java2D API
	public static int relativeCCW(double x1, double y1,
            double x2, double y2,
            double px, double py) {
		x2 -= x1;
		y2 -= y1;
		px -= x1;
		py -= y1;
		double ccw = px * y2 - py * x2;
		if (ccw == 0.0) {
		// The point is colinear, classify based on which side of
		// the segment the point falls on.  We can calculate a
		// relative value using the projection of px,py onto the
		// segment - a negative value indicates the point projects
		// outside of the segment in the direction of the particular
		// endpoint used as the origin for the projection.
		ccw = px * x2 + py * y2;
		if (ccw > 0.0) {
		// Reverse the projection to be relative to the original x2,y2
		// x2 and y2 are simply negated.
		// px and py need to have (x2 - x1) or (y2 - y1) subtracted
		//    from them (based on the original values)
		// Since we really want to get a positive answer when the
		//    point is "beyond (x2,y2)", then we want to calculate
		//    the inverse anyway - thus we leave x2 & y2 negated.
		px -= x2;
		py -= y2;
		ccw = px * x2 + py * y2;
		if (ccw < 0.0) {
		ccw = 0.0;
		}
		}
		}
		return (ccw < 0.0) ? -1 : ((ccw > 0.0) ? 1 : 0);
	}
	
	//From Java2D API
	public static boolean linesIntersect(double x1, double y1,
            double x2, double y2,
            double x3, double y3,
            double x4, double y4) {
		return ((relativeCCW(x1, y1, x2, y2, x3, y3) *
		relativeCCW(x1, y1, x2, y2, x4, y4) <= 0)
		&& (relativeCCW(x3, y3, x4, y4, x1, y1) *
		relativeCCW(x3, y3, x4, y4, x2, y2) <= 0));
	}
	
	public static float linelength(int x1, int y1, int x2, int y2) {
		return (float) Math.sqrt(Math.pow((x2-x1), 2) + Math.pow((y2-y1), 2));
	}
	
	private Polyline createWall(int x1, int y1, int x2, int y2)
	{
		int height = URWnode.INITIAL_Y;
		SimpleVector testEdge[] = new SimpleVector[2];
		
		testEdge[0] = new SimpleVector(x1, y1, height);
		testEdge[1] = new SimpleVector(x2, y2, height);
	
		
		Polyline temp = new Polyline(testEdge, new RGBColor(50, 60, 80));
		temp.setWidth(20);
		return temp;
	}
}

